package cn.y.operation.application.web.service;

import cn.y.operation.application.core.entity.SysUser;
import cn.y.operation.application.web.mapper.SysUserMapper;
import cn.y.operation.framework.constants.AuthKeyConstants;
import cn.y.operation.framework.constants.UserConstants;
import cn.y.operation.framework.utils.database.RedisUtils;
import cn.y.operation.framework.utils.dev.DateUtils;
import cn.y.operation.framework.utils.dev.StringUtils;
import cn.y.operation.framework.utils.encryption.MD5Utils;
import cn.y.operation.framework.utils.encryption.PasswordUtils;
import cn.y.operation.framework.utils.system.ShiroUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 用户信息表(SysUser)表服务实现类
 *
 * @author Y
 * @since 2021-04-19 18:54:37
 */
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {
    Logger logger = LoggerFactory.getLogger(SysUserServiceImpl.class);
    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public SysUser getById(Long userId) {
        return this.sysUserMapper.getById(userId);
    }

    /**
     * 检查登录名唯一
     */
    @Override
    public SysUser checkLoginNameUnique(String loginName) {
        return this.sysUserMapper.checkLoginNameUnique(loginName);
    }

    /**
     * 新增数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */
    @Override
    public SysUser insert(SysUser sysUser) {
        this.sysUserMapper.insert(sysUser);
        return sysUser;
    }

    @Override
    public String registered(SysUser user) {
        String msg = "", loginName = user.getLoginName(), password = user.getPassword();
        if (StringUtils.isEmpty(loginName)) {
            msg = "用户名不能为空";
        } else if (loginName.length() < UserConstants.USERNAME_MIN_LENGTH || loginName.length() > UserConstants.USERNAME_MAX_LENGTH) {
            msg = "账户长度必须在2到20个字符之间";
        } else if (this.checkLoginNameUnique(loginName) != null) {
            msg = "保存用户'" + loginName + "'失败，注册账号已存在";
        } else if (StringUtils.isEmpty(password)) {
            msg = "用户密码不能为空";
        } else if (password.length() < UserConstants.PASSWORD_MIN_LENGTH || password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
            msg = "密码长度必须在5到20个字符之间";
        } else {
            user.setCreateBy("Admin");
            user.setCreateTime(DateUtils.getDate());
            user.setPwdUpdateDate(DateUtils.getDate());
            user.setUserName(loginName);
            user.setSalt(ShiroUtils.randomSalt());
            user.setPassword(PasswordUtils.encryptPassword(user.getSalt(), MD5Utils.MD5(user.getPassword())));
            boolean regFlag = true;//this.sysUserMapper.registered(user) > 0;
            if (regFlag) {
                logger.info("----------------------------------------------");
                RedisUtils.set(AuthKeyConstants.REGISTERED + loginName, StringUtils.random(5), 60 * 15);
                //EmailUtils.sendThymeleafMail();
            } else {
                msg = "注册失败,请联系系统管理人员";
            }
        }
        return msg;
    }

    @Override
    public String verifyRegistration(String loginName, String verificationCode) {
        String result = "";
        String code = RedisUtils.get(AuthKeyConstants.REGISTERED + loginName);
        if (StringUtils.isEmpty(code)) {
            result = "验证码已过期或不存在";
        } else {
            if (!code.equals(verificationCode)) {
                result = "验证码错误";
            }//else { RedisUtils.delete(AuthKeyConstants.REGISTERED + loginName); }
        }
        return result;
    }

    @Override
    public boolean activateUser(String loginName) {
        return true;
    }

    /**
     * 修改数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */
    @Override
    public SysUser update(SysUser sysUser) {
        this.sysUserMapper.update(sysUser);
        return this.getById(sysUser.getUserId());
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long userId) {
        return this.sysUserMapper.deleteById(userId) > 0;
    }
}
